<?php
namespace  app\super\controller;
use think\controller;
use  think\Db;
class OtherController extends CommonController {
    /**
     * 获取意见反馈列表
     */
    public function getFeekList(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['u.user_username|u.user_phone|a.advice_content'] = array('like',"%$keyword%");
        }

        if($_POST['start']!=''){//时间
            $where['a.advice_time'] =array('>=',$_POST['start']);
        }
        if($_POST['end']!=''){//时间
            $where['a.advice_time'] =array('<=',$_POST['end']);
        }

        $res = Db::table('up_advice')->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('u.user_username,user_phone,a.*')
            ->order('a.advice_time DESC')
            ->select();
        $total = Db::table('up_advice')->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    public function feedInfo(){
        $cid = $_POST['cid'];
        $where['a.advice_id'] = $cid;
        $res = Db::table('up_advice')->alias('a')
            ->join('up_u_user u','u.user_id = a.user_id','left')
            ->where($where)
            ->field('u.user_username,user_phone,a.*')
            ->find();
        $this->assign('res',$res);
        return $this->fetch('other/feedInfo');
    }
    /**
     * 版本管理
     */
    public function getEditionList(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table('up_edition')
            ->limit($rows * ($page - 1), $rows)
            ->order('edition_time DESC')
            ->select();
        $total = Db::table('up_edition')
            ->limit($rows * ($page - 1), $rows)
            ->order('edition_time DESC')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * 添加版页面本
     */
    public function addEdition(){
        return $this->fetch('other/addEdition');
    }

    /***
     * 添加版本功能
     */
    public function insertEdition(){
        if(trim($_POST['edition_name'])==''||trim($_POST['edition_content'])==''){
            $returnData['status'] = 0;
            $returnData['msg'] = "新增版本信息不能为空";
            return json_encode($returnData);
        }else{
            $data['edition_name'] = $_POST['edition_name'];
            $data['edition_content'] = $_POST['edition_content'];
            $data['edition_id'] = $this->getRandChar(32);
            $data['edition_time'] = date('Y-m-d H:i:s');
            $data['edition_type'] = $_POST['edition_type'];
            $res = Db::table('up_edition')->insert($data);

            if($res){
                $whereU['user_status'] = 1;
                $up_u_user =Db::table('up_u_user')->where($whereU)->column('user_id');
                $msg1 = '';
                $msg = '系统发布新版本了'.$_POST['edition_name'].'此版本优化了'.$_POST['edition_content'].'更多精彩请下载查看';
                $msg_data = array();
                $this->sendJpush(2,$msg,$msg_data,$up_u_user,$msg1,'',1,'');
                $returnData['status'] = 1;
                $returnData['msg'] = "版本发布成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "版本发布失败";
                return json_encode($returnData);
            }
        }
    }


    /***
     * 规则配置
     */
    public function rule(){
        $res = Db::table('up_app_config')
            ->select();
        $total = Db::table('up_app_config')
               ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * 规则修改
     */
    public function updateRule(){
        $nid = $_POST['nid'];
        $res = Db::table('up_app_config')->where('up_base_id',$nid)->find();
        $this->assign('res',$res);
        return $this->fetch('other/updateRule');
    }

    /**
     * 编辑功能
     */
    public function insertRulerInfo(){
        $data['up_info_count'] = $_POST['up_info_count'];
        $data['up_adv_position_money'] = $_POST['up_adv_position_money'];
        $data['up_banner_adv_single_money'] = $_POST['up_banner_adv_single_money'];
        $data['up_banner_percent'] = $_POST['up_banner_percent'];
        $data['up_common_adv_single_money'] = $_POST['up_common_adv_single_money'];
        $data['up_common_percent'] = $_POST['up_common_percent'];
        $data['up_red_percent'] = $_POST['up_red_percent'];
        $res = Db::table('up_app_config')->where('up_base_id',$_POST['up_base_id'])->update($data);

        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "编辑成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "编辑失败";
            return json_encode($returnData);
        }
    }

    /**
     * 系统公告
     */
    public function bulletin(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table('up_bulletin')
            ->limit($rows * ($page - 1), $rows)
            ->order('bulletin_time DESC')
            ->select();
        $total = Db::table('up_bulletin')
            ->limit($rows * ($page - 1), $rows)
            ->order('bulletin_time DESC')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    public function trainInfo(){
        $cid = $_POST['cid'];
        $up_bulletin = Db::table('up_bulletin')->where('bulletin_id',$cid)->find();
        $this->assign('bulletin',$up_bulletin);
        return $this->fetch('other/trainInfo');
    }
    /***
     * 添加系统公告
     */
    public function addBulletin(){
        return $this->fetch('other/addBulletin');
    }

    /***
     * @return string
     * 系统公告添加功能
     */
    public function insertBulletin(){
        $uid = session('loginSession')['user_id'];

        $data['bulletin_object'] = $_POST['bulletin_object'];
        $data['bulletin_content'] = $_POST['bulletin_content'];
        $data['bulletin_time'] = date('Y-m-d H:i:s');
        $data['uuid'] = $uid;
        $data['bulletin_id'] = $this->getRandChar(32);
        $res = Db::table('up_bulletin')->insert($data);

        if($res){
            $whereU['user_status'] = 2;
            $up_u_user =Db::table('up_u_user')->where($whereU)->column('user_id');
            $msg1 = '';
            $msg = $_POST['bulletin_content'];
            $msg_data = array();
            $this->sendJpush(2,$msg,$msg_data,$up_u_user,$msg1,'',1,'');
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败";
            return json_encode($returnData);
        }
    }

    /**
     * 获取行业分类信息
     */
    public function getIndustryInfo(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $res = Db::table('up_dic_region')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('up_dic_region')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /***
     * 删除行业分类信息
     */
    public function delIndustry(){
        $cid = $_POST['cid'];
        $res = Db::table('up_dic_region')->where('region_id',$cid)->delete();
        if($res){
            $returnData['status'] = 1;
            $returnData['msg'] = "删除成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "删除失败";
            return json_encode($returnData);
        }
    }

    /**
     * 添加分类页面
     */
    public function addIndustry(){
       return $this->fetch('other/addIndustry');
    }

    public function insertIndustry(){
        $where['region_name'] = $_POST['region_name'];
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $res = Db::table('up_dic_region')->where($where)->find();
        if($res['region_id']){
            $returnData['status'] = 0;
            $returnData['msg'] = "行业分类存在相同的行业，不能重复添加";
            return json_encode($returnData);
        }
        $data['region_name'] = $_POST['region_name'];
        $data['region_id'] = $this->getRandChar(32);
        $data['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $result = Db::table('up_dic_region')->insert($data);
        if($result){
            $returnData['status'] = 1;
            $returnData['msg'] = "添加成功";
            return json_encode($returnData);
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "添加失败";
            return json_encode($returnData);
        }
    }
    /**
     * 编辑页面
     */
    public function updateIndustry(){
        $res = Db::table('up_dic_region')->where('region_id',$_POST['nid'])->find();
        $this->assign('res',$res);
        return $this->fetch('other/updateIndustry');
    }
    /**
     * 编辑功能
     */
    public function updateIndustryInfo(){
        $where['region_id'] = array('NEQ',$_POST['region_id']);
        $where['region_fid'] = 'P48DLGW9He9A63HFNyBqPnXxXTvdclqb';
        $where['region_name'] = $_POST['region_name'];
        $res = Db::table('up_dic_region')->where($where)->find();
        if($res['region_id']){
            $returnData['status'] = 0;
            $returnData['msg'] = "行业分类存在相同的行业，不能重复添加";
            return json_encode($returnData);
        }else{
            $result = Db::table('up_dic_region')->where('region_id',$_POST['region_id'])->setField('region_name',$_POST['region_name']);
            if($result){
                $returnData['status'] = 1;
                $returnData['msg'] = "编辑成功";
                return json_encode($returnData);

            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "编辑失败";
                return json_encode($returnData);
            }
        }
    }
}